{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Hash\n",
    "\n",
    "* 値に名前をつける"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Hashリテラル\n",
    "\n",
    "* 両側を `{}` で囲んでキーと値の組をカンマで区切って指定する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"foo\"=>1, \"bar\"=>2}\n",
      "1\n",
      "{:haga=>5, :hage=>6}\n",
      "{:hoge=>7, :fuga=>8}\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "a = {\"foo\" => 1, \"bar\" => 2}\n",
    "c = {:haga => 5, :hage => 6}\n",
    "d = {hoge: 7, fuga: 8}\n",
    "\n",
    "puts a\n",
    "puts a[\"foo\"]\n",
    "puts c\n",
    "puts d\n",
    "puts d[:fuga]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 存在しないキーの指定\n",
    "\n",
    "* nilが返る"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nil\n"
     ]
    }
   ],
   "source": [
    "a = {\"foo\" => 1, \"bar\" => 2}\n",
    "p a[\"baz\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Hashのデフォルト値の設定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "デフォルト値はfuga\n",
      "{}\n",
      "\"fuga\"\n",
      "3を代入\n",
      "{:foo=>3}\n"
     ]
    }
   ],
   "source": [
    "puts \"デフォルト値はfuga\"\n",
    "a = Hash.new(\"fuga\")\n",
    "p a\n",
    "p a[:foo]\n",
    "\n",
    "puts \"3を代入\"\n",
    "a[:foo] = 3\n",
    "puts a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# その他Hashの作成方法\n",
    "\n",
    "* Hashのクラスメソッド `[]` を利用\n",
    "* Array#to_hを利用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"x\"=>3, \"y\"=>4}\n",
      "{:m=>9, :n=>10}\n"
     ]
    }
   ],
   "source": [
    "b = Hash[\"x\", 3, \"y\", 4]\n",
    "f = [[:m,9], [:n,10]].to_h\n",
    "\n",
    "puts b\n",
    "puts f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# ハッシュ引数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{:foo1=>1, :foo2=>2}\n",
      "{:foo1=>1, :foo2=>2}\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "def foo a, b, c\n",
    "  c\n",
    "end\n",
    "\n",
    "### 4つの引数を渡しているように見えるが、実際は3つ\n",
    "### この書き方は最後がHashのときのみ有効\n",
    "puts foo(1, 2, :foo1=>1, :foo2=>2)\n",
    "\n",
    "### 省略せずに{}を記載すると以下のようになる\n",
    "puts foo(1, 2, {:foo1=>1, :foo2=>2})\n",
    "\n",
    "### 最初にHashを持ってくる場合は{}を必ず付ける必要がある\n",
    "puts foo({:foo1=>1, :foo2=>2}, 3, 4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{:a=>1, :b=>2}]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def func *a\n",
    "  a\n",
    "end\n",
    "\n",
    "func({a: 1, b: 2})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
